// pages/myfeedback/myfeedback.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
var app = getApp();
var a = false
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username: '',
        mobile: '',
        text: '',
        userid: '',
        photos: []
    },
    usernameInput: function(e) {
        this.setData({
            username: e.detail.value
        })
    },
    usermobile: function(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    usertext: function(e) {
        this.setData({
            text: e.detail.value
        })
    },
    addimg: function() {
        a = true
        api.chooseimage(3, this.data.photos, 'file', app.u.HOME_URL + app.u.UPLOADIMG).then(res => {
            if (this.data.photos.length) {
                this.setData({
                    photos: this.data.photos.concat(res)
                })
            } else {
                this.setData({
                    photos: res
                })
            }

        })
    },
    giftview: function(e) {
        a = true
        let touchTime = this.data.touch_end - this.data.touch_start
        if (touchTime > 350) {
            let that = this
            let images = that.data.photos
            var index = e.currentTarget.dataset.index
            wx.showModal({
                title: '提示',
                content: '确定要删除此图片吗？',
                success(res) {
                    if (res.confirm) {
                        images.splice(index, 1)
                        that.setData({
                            photos: images
                        })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        } else {
            wx.previewImage({
                current: e.currentTarget.dataset.url,
                urls: this.data.photos
            })
        }
    },
    dologin: function() {
        var that = this
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (that.data.username == '') {
            api.showError("姓名不能为空")
            return;
        }
        if (that.data.mobile == 0 || that.data.mobile == '') {
            api.showError("手机号不能为空")
            return;
        }
        if (!myreg.test(that.data.mobile)) {
            api.showError("手机号输入有误")
            return;
        }
        if (that.data.text == 0 || that.data.text == '') {
            api.showError("建意与意见不能为空")
            return;
        }
        if (that.data.text.length < 6) {
            api.showError("建意与意见至少输入六个字")
            return;
        }
        if (that.data.photos.length == 0) {
            api.showError("请上传图片")
            return;
        }
        var data = {
            mobile: that.data.mobile,
            account_id: that.data.userid,
            content: that.data.text,
            photos: that.data.photos.join(',')
        }
        var url = app.u.HOME_URL + app.u.FEEDBACK
        api.requestUrl(data, url).then(res => {
            api.showError("提交成功")
            that.setData({
                username: '',
                mobile: '',
                text: '',
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                that.setData({
                    userid: res.data.id,
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        if (a) {
            a = false;
            return;
        }
        wx.showShareMenu({
            withShareTicket: true
        })
    },

})